package com.shunwang.errors.engine.builder.annotation;

import java.io.File;
import java.lang.reflect.Method;

import com.shunwang.errors.engine.builder.bean.NodeBean;
import com.shunwang.errors.engine.builder.bean.NodeMethod;
import com.shunwang.errors.engine.handler.Handler;
import com.shunwang.errors.engine.util.AnnotationUtil;
import com.shunwang.errors.engine.util.ClazzUtil;

public class LoggerHandlerAnnotationParser extends LoggerAnnotationParser {

	public LoggerHandlerAnnotationParser(File file){
		super(file);
	}
	
	public LoggerHandlerAnnotationParser(String filePath){
		super(filePath);
	}
	
	protected void mapperBean(Method method,String className) throws Exception{
		LoggerHandler handler = (LoggerHandler)AnnotationUtil.getAnnotationMethod(method, LoggerHandler.class);
		if(handler != null){
		    Handler handlerProc =  (Handler)ClazzUtil.newInstance(handler.handler());
		    Class[] handlerClasses = handler.throwable();
		    for(Class handlerClass:handlerClasses){
    			Throwable throwable = (Throwable)ClazzUtil.newInstance(handlerClass);
    			String methodName = method.getName();
    			loadBeans(throwable,handlerProc,className,methodName);
		    }
		}
	}
	
	protected void loadBeans(Throwable throwable,Handler handler,String className,String methodName){
		NodeBean nodeBean = nodeBeans.getNodeBean(className);
		if(nodeBean == null){
			nodeBean = new NodeBean();
			nodeBean.setClazz(className);
			nodeBeans.setList(nodeBean);
		}
		NodeMethod method = nodeBean.getNodeMethod(methodName, handler.getClass().getName());
		if(method == null){
			method = new NodeMethod();
			method.setHandClazz(handler.getClass().getName());
			method.setMethodName(methodName);
			nodeBean.setMethod(method);
		}
		if(!method.contains(throwable.getClass().getName())){
			method.getExClazz().add(new StringBuffer(throwable.getClass().getName()));
		}
	}

}
